﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Autodesk.Revit.DB;

namespace YArchitech.HVAC
{
	public class IndoorLoop
	{
		public IndoorLoop()
		{
			this.ConnType = 1;
			this.RateUnbalance = 0.0;
			this.loops = new EquipmentLoopSet();
			this.supplyPipes = new PipeSet();
			this.backPipes = new PipeSet();
		}

		public int Index
		{
			get
			{
				return this.index;
			}
			set
			{
				this.index = value;
			}
		}

		public string Name
		{
			get
			{
				return "环路" + this.index;
			}
		}

		public EquipmentLoopSet EquipmentLoops
		{
			get
			{
				return this.loops;
			}
		}

		public PipeSet SupplyPipes
		{
			get
			{
				return this.supplyPipes;
			}
		}

		public PipeSet BackPipes
		{
			get
			{
				return this.backPipes;
			}
		}

		public int ConnType { get; set; }

		public IList<Element> Equipments
		{
			get
			{
				return this.GetAllEquipments();
			}
		}

		public IList<Element> Elements
		{
			get
			{
				return this.GetAllElements();
			}
		}

		public double Resistance
		{
			get
			{
				return this.resistance;
			}
		}

		public double RateUnbalance { get; set; }

		public int WorstIndex
		{
			get
			{
				return this.worstIndex;
			}
		}

		public void Add(EquipmentLoop loop)
		{
			if (!this.Contains(loop))
			{
				this.loops.Add(loop);
			}
		}

		public void Add(HyCalcPipe pipe, bool bSupply = false)
		{
			if (bSupply)
			{
				this.supplyPipes.Add(pipe);
				return;
			}
			this.backPipes.Add(pipe);
		}

		public EquipmentLoop GetAt(int loopIndex)
		{
			return (from loop in this.loops
			where loop.Index == loopIndex
			select loop).First<EquipmentLoop>();
		}

		private List<Element> GetAllEquipments()
		{
			return (from equipmentLoop in this.loops
			select equipmentLoop.Equipment).ToList<Element>();
		}

		private bool Contains(EquipmentLoop loop)
		{
			List<ElementId> list = (from equipment in this.GetAllEquipments()
			select equipment.Id).ToList<ElementId>();
			return list.Count != 0 && list.Contains(loop.Equipment.Id);
		}

		public Dictionary<string, double> CalcUnbalanceRate()
		{
			Dictionary<string, double> dictionary = new Dictionary<string, double>();
			for (int i = 1; i <= this.loops.Count; i++)
			{
				EquipmentLoop at = this.GetAt(i);
				double num = at.Resistance;
				num += this.supplyPipes.SumResistance(i);
				int endIndex = (this.ConnType == 0) ? (this.loops.Count + 1 - i) : i;
				num += this.backPipes.SumResistance(endIndex);
				double num2 = Math.Abs((num - this.resistance) / this.resistance);
				at.RateUnbalance = num2;
				dictionary.Add(at.Name, num2);
				Log.Info(new string[]
				{
					string.Format("Loop {0} Resistance = {1} Unbalance={2}", at.Name, num, num2)
				});
			}
			return dictionary;
		}

		public double CalcResistance()
		{
			for (int i = 1; i <= this.loops.Count; i++)
			{
				double num = this.GetAt(i).CalcResistance();
				num += this.supplyPipes.SumResistance(i);
				int endIndex = (this.ConnType == 0) ? (this.loops.Count + 1 - i) : i;
				num += this.backPipes.SumResistance(endIndex);
				if (num > this.resistance)
				{
					this.resistance = num;
					this.worstIndex = i;
				}
			}
			Log.Info(new string[]
			{
				this.Name + ": resistance = " + this.resistance
			});
			return this.resistance;
		}

		private List<Element> GetAllElements()
		{
			List<Element> list = new List<Element>();
			foreach (EquipmentLoop equipmentLoop in this.loops)
			{
				list.AddRange(equipmentLoop.Elements);
			}
			return new HashSet<Element>(list).ToList<Element>();
		}

		public TreeNode ToTreeNode()
		{
			TreeNode treeNode = new TreeNode(this.Name);
			treeNode.Tag = this;
			TreeNode treeNode2 = new TreeNode("供水干管");
			treeNode2.Tag = this.SupplyPipes;
			TreeNode treeNode3 = new TreeNode("回水干管");
			treeNode3.Tag = this.BackPipes;
			TreeNode treeNode4 = new TreeNode("散热器");
			treeNode4.Tag = this.EquipmentLoops;
			treeNode.Nodes.AddRange(new TreeNode[]
			{
				treeNode2,
				treeNode3,
				treeNode4
			});
			return treeNode;
		}

		public void UpDate()
		{
			this.supplyPipes.UpDate();
			this.backPipes.UpDate();
			this.loops.ForEach(delegate(EquipmentLoop loop)
			{
				loop.Update();
			});
		}

		public void BackRevit()
		{
			this.supplyPipes.BackRevit();
			this.backPipes.BackRevit();
			this.loops.ForEach(delegate(EquipmentLoop loop)
			{
				loop.BackRevit();
			});
		}

		private int index = 1;

		private EquipmentLoopSet loops;

		private PipeSet supplyPipes;

		private PipeSet backPipes;

		private double resistance;

		private int worstIndex = 1;
	}
}
